package com.zzxx.exam.ui;

import com.zzxx.exam.entity.QuestionInfo;
import com.zzxx.exam.service.Controller;

import javax.swing.*;

public class FrameManager {
    private final Controller controller;
    private final ExamFrame examFrame;
    private final LoginFrame loginFrame;
    private final MenuFrame menuFrame;
    private final MsgFrame msgFrame;
    private final WelcomeWindow welcomeWindow;
    private final HistoryFrame historyFrame;
    //规则
    private final String rule;

    public FrameManager(Controller contrl){
        loginFrame = new LoginFrame(this);
        menuFrame = new MenuFrame(this);
        examFrame = new ExamFrame(this);
        msgFrame = new MsgFrame();
        welcomeWindow = new WelcomeWindow(this);
        this.controller = contrl;
        rule = controller.getRule();
        historyFrame = new HistoryFrame();
        historyFrame.setTitle("历史成绩");
    }
    public void login(){
        //登录方法
        controller.login(loginFrame.getIdField(), loginFrame.getPwdField());


    }

    //启动程序，并在等待后进入登录页面
    public void startSystem(){
        welcomeWindow.setVisible(true);
        try {
            Thread.sleep(Controller.WAITE_TIME);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        welcomeWindow.setVisible(false);
        loginFrame.setVisible(true);
    }


    //开始考试
    public void startExam() {
        controller.startExam();
    }
    //考试页面
    public void showExam(){
        //界面切换,并更新首道题目
        menuFrame.setVisible(false);
        examFrame.setVisible(true);
    }
    public void initFace(){
        //传入初始题目 和考试信息
        examFrame.setNowQuestionInfo(controller.getNowQuestionInfo());
        examFrame.setNowExamInfo(controller.getNowExamInfo());
        //初始化时间信息，给线程点准备时间
        long deadline = controller.getNowExamInfo().getTimeLimit() * 60L; //化成秒
        String hour = String.valueOf(deadline / 60L / 60L);
        String min = String.valueOf(deadline % (60L * 60) / 60L);
        String sec = String.valueOf(deadline % 60L);
        examFrame.updateTime(hour, min, sec);
        //传入用户
        examFrame.getExamInfo().setText(examFrame.getNowExamInfo().toString());
        refreshExamFace();
    }

    //展示规则
    public void showRule() {
        msgFrame.getTa().setText(controller.getRule());
        msgFrame.setVisible(true);
    }

    //展示历史成绩
    public void showHistory() {
        historyFrame.getTa().setText(controller.getHistory());
        historyFrame.setVisible(true);
    }

    //进入菜单
    public void showMenu(){
        loginFrame.setVisible(false);
        menuFrame.getInfo().setText(controller.getUserName() + " 同学您好!");
        menuFrame.setVisible(true);
    }
    //结束考试,切回菜单
    public void overExam() {
        examFrame.setVisible(false);
        menuFrame.setVisible(true);
    }

    //上一题
    public void prev() {
        controller.prev();
    }

    //下一题
    public void next() {
        controller.next();
    }

    //更新题目显示
    private void refreshQuestion() {
        QuestionInfo nowQuestionInfo = controller.getNowQuestionInfo();
        //开卷考
        String answer = "当前题目答案:" + nowQuestionInfo.getQuestion().getAnswers();
        answer = answer.replace("0", "A");
        answer = answer.replace("1", "B");
        answer = answer.replace("2", "C");
        answer = answer.replace("3", "D");
        System.out.println(answer);
        //给考试界面当前题目赋值
        examFrame.setNowQuestionInfo(nowQuestionInfo);
        //题号 + 题目
        examFrame.getQuestionArea().setText(nowQuestionInfo.getQuestionIndex() + ". " + nowQuestionInfo);
        examFrame.refreshCheckBox(); //清空复选框
    }

    //交卷方法
    public void submit() {
        controller.setSubmitEarly();
        controller.submit();
    }

    public JLabel getMessagePanel() {
        return loginFrame.getMessage();
    }


    //获取当前
    public QuestionInfo getNowQuestionInfo() {
        //打包现在的题目
        return examFrame.getNowQuestion();
    }

    //刷新考试页面
    public void refreshExamFace() {
        //更新题目
        refreshQuestion();
        //更新题号
        examFrame.refreshQuesIndex();
    }

    public void refreshExamTime(String hour, String min, String sec) {
        //更新剩余时间
        examFrame.updateTime(hour, min, sec);
    }
}
